package com.snake.vargoappstore.widget;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.snake.mylibrary.utils.ApkUtils;
import com.snake.vargoappstore.R;
import com.snake.vargoappstore.db.LocalAppInfo;

import java.util.Random;

public class LocalListViewItem extends FrameLayout {
    private Context mContext;
    private TextView mTitle;
    private TextView mDesc;
    private RatingBar mStar;
    private Button mButton;
    private int mPosition;
    private LocalAppInfo mLocalAppInfo;
    private ImageView mIcon;
    private TextView mTextView1;
    private TextView mTextView2;

    public LocalListViewItem(Context context) {
        super(context);
        init(context);
    }

    public LocalListViewItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LocalListViewItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        inflate(context, R.layout.listview_item_main_local, this);
        mIcon = (ImageView) findViewById(R.id.iv_listView_item_icon);
        mTitle = (TextView) findViewById(R.id.tv_listView_item_title);
        mDesc = (TextView) findViewById(R.id.tv_listView_item_desc);
        mStar = (RatingBar) findViewById(R.id.ratingB_listView_item_star);
        mButton = (Button) findViewById(R.id.btn_listView_item_install);
        mButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sendTip();
//                uninstallApk();
//                startApp();
            }
        });
        mTextView1 = (TextView) findViewById(R.id.item_tv_1);
        mTextView2 = (TextView) findViewById(R.id.item_tv_2);
    }

    private void startApp() {
        ApkUtils.startApp(mContext,mLocalAppInfo.getPackageName());
    }

    private void uninstallApk() {
        ApkUtils.uninstallApk(mContext,mLocalAppInfo.getPackageName());
    }


    private void sendTip() {
        Intent intent = new Intent(TabItem.TABITEM_TIP+mPosition);
        intent.putExtra("tip",new Random().nextInt(99));
        LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
    }

    public void bindData(int position ,LocalAppInfo data){
        mPosition = position;
        mLocalAppInfo = data;
        mTitle.setText(data.getName());
        mDesc.setText(data.getPackageName());
//        mStar.setRating(data.getVersionCode());
        mIcon.setImageDrawable(data.getIcon());
        mTextView1.setText(data.getVersionName());
//        mTextView2.setText(data.getFlag());
    }
}
